﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyGraphics
{
    public partial class Form1 : Form
    {
        private string curFileName;
        private System.Drawing.Bitmap curBitmap;
        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 打开图像
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Open_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "所有图片｜*.bmp;*.pcx;*.png;*,jpg;*.gif|";
            open.Title = "打开文件";
            if (open.ShowDialog() == DialogResult.OK)
            {
                curFileName = open.FileName;
                try
                {
                    curBitmap = (Bitmap)Image.FromFile(curFileName);
                }
                catch (Exception exp)
                {
                    MessageBox.Show(exp.Message);
                }
            }
            Invalidate();//对窗体进行重新绘制，这将强行执行paint事件处理程序
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            if (curBitmap != null)
            {
                g.DrawImage(curBitmap, 200, 20, curBitmap.Width / 2, curBitmap.Height / 2);
                Rectangle rect = new Rectangle(200, 60 + curBitmap.Height / 2, curBitmap.Width / 2, curBitmap.Height / 2);
                g.DrawImage(curBitmap, rect, 0, 0, curBitmap.Width, curBitmap.Height, GraphicsUnit.Pixel);
            }
        }
        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// 放大图像
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Amplify_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            float x = (float)double.Parse(zoomText.Text);
            SolidBrush brush = new SolidBrush(Color.White);
            Rectangle rect = new Rectangle(200, 20, curBitmap.Width / 2, curBitmap.Height / 2);
            g.FillRectangle(brush, rect);

            if (curBitmap != null)
            {
                g.ScaleTransform(x, x);
                g.DrawImage(curBitmap, 200, 20, curBitmap.Width / 2, curBitmap.Height / 2);
            }
            brush.Dispose();

        }
        /// <summary>
        /// 缩小图像
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Reduce_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();   //创建画图对象并创建画布
            //创建画刷并指定 颜色
            SolidBrush brush = new SolidBrush(Color.White);
            //选择指定区域
            Rectangle rect = new Rectangle(200, 20, curBitmap.Width / 2, curBitmap.Height / 2);
            //在指定区域填充指定颜色
            g.FillRectangle(brush, rect);
            //获取缩放比例
            float x = (float)double.Parse(zoomText.Text);
            if (curBitmap != null)
            {
                //按给出的缩放比例缩小
                Graphics g1 = pictureBox1.CreateGraphics();
                g1.FillRectangle(brush, pictureBox1.ClientRectangle);
                //g.ScaleTransform(x, x);
                //rect = new Rectangle(200, 20, curBitmap.Width/2, curBitmap.Height/2);
                // DrawImage(Image, Rectangle, Rectangle, GraphicsUnit)  在指定位置并且按指定大小绘制指定的 Image 的指定部分。
                //g.DrawImage(curBitmap, rect, 0, 0, curBitmap.Width, curBitmap.Height, GraphicsUnit.Pixel);
                g1.DrawImage(curBitmap, pictureBox1.ClientRectangle, 120, 50, curBitmap.Width, curBitmap.Height, GraphicsUnit.Pixel);
            }
        }
        /// <summary>
        /// 图像平移
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Translation_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            float x = (float)double.Parse(translationText.Text);
            float y = (float)double.Parse(yoffText.Text);
            //指定填充的颜色
            SolidBrush brush = new SolidBrush(Color.White);
            //选择指定区域
            Rectangle rect = new Rectangle(200, 20, curBitmap.Width / 2, curBitmap.Height / 2);
            //在指定区域填充指定颜色
            g.FillRectangle(brush, rect);
            if (curBitmap != null)
            {
                g.TranslateTransform(x, y);
                g.DrawImage(curBitmap, 200, 20, curBitmap.Width / 2, curBitmap.Height / 2);
            }

        }
        /// <summary>
        /// 图像旋转
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Rotate_Click(object sender, EventArgs e)
        {
            //Graphics g = this.CreateGraphics();
            ////指定填充的颜色
            //SolidBrush brush = new SolidBrush(Color.White);
            ////选择指定区域
            //Rectangle rect = new Rectangle(200, 20, curBitmap.Width / 2, curBitmap.Height / 2);
            ////在指定区域填充指定颜色
            //g.FillRectangle(brush, rect);
            ////获取旋转的角度
            //float x = (float)double.Parse( angleText.Text);
            //if (curBitmap != null)
            //{
            //    g.RotateTransform(x);
            //    //rect = new Rectangle(200, 20, curBitmap.Width / 2, curBitmap.Height / 2);
            //    g.DrawImage(curBitmap, rect, 0, 0, curBitmap.Width, curBitmap.Height, GraphicsUnit.Pixel);

            //}


            //修改后的代码
            Graphics g = this.CreateGraphics();
            ////指定填充的颜色
            SolidBrush brush = new SolidBrush(Color.White);
            //选择指定区域
            Rectangle rect = new Rectangle(200, 20, curBitmap.Width / 2, curBitmap.Height / 2);
            //在指定区域填充指定颜色
            g.FillRectangle(brush, rect);
            ////获取旋转的角度
            float x = (float)double.Parse(angleText.Text);
            double radian = x * Math.PI / 180.0;
            double a = (double)Math.Abs(curBitmap.Width / 2 * Math.Cos(radian));
            double b = (double)Math.Abs(curBitmap.Width / 2 * Math.Sin(radian));
            double c = (double)Math.Abs(curBitmap.Height / 2 * Math.Sin(radian));
            double d = (double)Math.Abs(curBitmap.Height / 2 * Math.Cos(radian));
            Point[] destinationPoints = new Point[3];
            if (x >= 0 && x <= 90)
            {
                destinationPoints[0] = new Point(200 + (int)c, 20);               // 原图左上角坐标
                destinationPoints[1] = new Point(200 + (int)a + (int)c, 20 + (int)b);// 原图右上角坐标
                destinationPoints[2] = new Point(200, 20 + (int)d); // 原图左下角坐标
            }
            else if (x == 180)
            {
                destinationPoints[0] = new Point(200 + curBitmap.Width / 2, 20 + curBitmap.Height / 2);    // 原图左上角坐标
                destinationPoints[1] = new Point(200, 20 + curBitmap.Height / 2);// 原图右上角坐标
                destinationPoints[2] = new Point(200 + curBitmap.Width / 2, 20);   // 原图左下角坐标
            }
            else if (x == -180)
            {
                destinationPoints[0] = new Point(200 + curBitmap.Width / 2, 20 + curBitmap.Height / 2);  // 原图左上角坐标
                destinationPoints[1] = new Point(200, 20 + curBitmap.Height / 2);// 原图右上角坐标
                destinationPoints[2] = new Point(200 + curBitmap.Width / 2, 20);     // 原图左下角坐标
            }
            else
            {
                destinationPoints[0] = new Point(200 + curBitmap.Width / 2, 20 + (int)d);               // 原图左上角坐标
                destinationPoints[1] = new Point(200 + curBitmap.Width / 2 - (int)a, 20 + (int)b + (int)d);// 原图右上角坐标
                destinationPoints[2] = new Point(200 + curBitmap.Width / 2 - (int)c, 20); // 原图左下角坐标
            }
            g.DrawImage(curBitmap, destinationPoints);

        }
        /// <summary>
        /// 选定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Choose_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            //指定填充的颜色
            SolidBrush brush = new SolidBrush(Color.White);
            //选择指定区域
            Rectangle rect = new Rectangle(200, 20, curBitmap.Width / 2, curBitmap.Height / 2);
            //在指定区域填充指定颜色
            g.FillRectangle(brush, rect);
            if (curBitmap != null)
            {
                RectangleF destRect = new RectangleF(200.0F, 20.0F, curBitmap.Width / 4F, curBitmap.Height / 4F);
                RectangleF srcRect = new RectangleF(120F, 50F, 300.0F, 200.0F);
                GraphicsUnit units = GraphicsUnit.Pixel;
                g.DrawImage(curBitmap, destRect, srcRect, units);

            }
        }
        /// <summary>
        /// 图像扭曲
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Angulation_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            //指定填充的颜色
            SolidBrush brush = new SolidBrush(Color.White);
            //选择指定区域
            Rectangle rect = new Rectangle(200, 20, curBitmap.Width / 2, curBitmap.Height / 2);
            //在指定区域填充指定颜色
            g.FillRectangle(brush, rect);
            if (curBitmap != null)
            {
                Point ulComer = new Point(200, 20);     //原图左上角坐标 
                Point urComer = new Point(curBitmap.Width / 2, 40);   //在图像右上角坐标
                Point llComer = new Point(curBitmap.Width / 2, curBitmap.Height / 2);   //原图像左下角坐标
                Point[] destPara = { ulComer, urComer, llComer };
                g.DrawImage(curBitmap, destPara);
            }

        }
    }   
}
